package com.maven.front.service.manageCourseDetail;

import com.maven.front.bean.EduCourse;
import com.maven.front.bean.EduWebsiteCourseDetail;
import com.maven.front.bean.Result;
import com.maven.front.dto.EduCourseDto;
import com.maven.front.mapper.EduCourseMapper;
import com.maven.front.mapper.EduWebsiteCourseDetailMapper;
import com.maven.front.mapper.SysSubjectMapper;
import com.maven.front.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * @descriptions: 对推荐课程模块进行操作
 * @author:
 * @date: 2020/8/13 16:41
 * @version: 1.0
 */

@Slf4j
@Service
public class CourseDetailService {

    @Resource
    private EduWebsiteCourseDetailMapper eduWebsiteCourseDetailMapper;
    @Resource
    private EduCourseMapper eduCourseMapper;
    @Resource
    private SysSubjectMapper sysSubjectMapper;


//    查询当前所有推荐课程数据
//    表  关系：
//            EduCourse课程信息表  EduWebsiteCourseDetail推荐课程表(只有ID关联字段)
    public Result selectAll(String courseName){
//        获取当前推荐表的数据
        //***
        List<EduWebsiteCourseDetail> list = eduWebsiteCourseDetailMapper.getList();
//        存储所有推荐课程信息
        List<EduCourseDto> listc = new ArrayList<EduCourseDto>();
//        循环获取推荐表中的课程ID
        for (EduWebsiteCourseDetail edu:list){
            //        继承类
            EduCourseDto eduCourseDto = new EduCourseDto();
            EduCourse eduCourse = new EduCourse();
            eduCourse.setCourseId(edu.getCourseId());
            if (courseName==null){
                log.info("查询所有，没有传递NAME的值到后台！");
            } else {
                eduCourse.setCourseName(courseName);
            }
            EduCourse eduCourse1 = eduCourseMapper.getCourse(eduCourse);
            if (ObjectUtils.isEmpty(eduCourse1)){
                log.info("当前推荐课程信息："+courseName+"--"+edu.getCourseId());
            } else {
                eduCourseDto.setTid(edu.getOrderNum()+"");
                BeanUtils.copy(eduCourse1, eduCourseDto);
//            将获取到的课程数据存储到lilstc集合中
                listc.add(eduCourseDto);
            }
        }
        if (CollectionUtils.isEmpty(listc)){
            return new Result(10004, "查询失败,暂无数据！", null);
        } else {
            return new Result(200, "查询成功！", listc);
        }
    }

    //    获取所有推荐课程
    public List<EduWebsiteCourseDetail> listAllDetail(){
        return eduWebsiteCourseDetailMapper.getList();
    }

    //    获取推荐课程表的所有数据
    public Result getList(){
        List<EduWebsiteCourseDetail> list = eduWebsiteCourseDetailMapper.getList();
        if (CollectionUtils.isEmpty(list)){
            return new Result(10100, "查询失败！", null);
        } else {
            return new Result(200, "查询成功！", list);
        }
    }


//    根据推荐课程ID删除推荐课程表的数据----不会删除课程信息,智慧删除推荐课程表的关联数据
    public Result delDetail(Integer courseId){
        int id = eduWebsiteCourseDetailMapper.selectEduWebsite(courseId).getId();
        int num = eduWebsiteCourseDetailMapper.delete(id);
        if (num > 0){
            return new Result(200, "删除成功！", null);
        } else {
            return new Result(10006, "删除失败！", null);
        }
    }

//    根据课程 ID获取信息
    public Result getWebsitCourse(Integer courseId){
        EduCourse eduCourse = eduCourseMapper.selectByPrimaryKey(courseId);
        EduWebsiteCourseDetail eduWebsiteCourseDetail = eduWebsiteCourseDetailMapper.selectEduWebsite(courseId);
        EduCourseDto eduCourseDto = new EduCourseDto();
        BeanUtils.copy( eduCourse, eduCourseDto);
        eduCourseDto.setTid(eduWebsiteCourseDetail.getOrderNum()+"");
        return new Result(200, "成功！", eduCourseDto);
    }

//    根据ID修改推荐课程的排序值
    public Result editDetail(Integer courseId, Integer orderNum){
        EduWebsiteCourseDetail eduWebsiteCourseDetail = new EduWebsiteCourseDetail();
        int id = eduWebsiteCourseDetailMapper.selectEduWebsite(courseId).getId();
        eduWebsiteCourseDetail.setId(id);
        eduWebsiteCourseDetail.setOrderNum(orderNum);
        int num = eduWebsiteCourseDetailMapper.updateByPrimaryKeySelective(eduWebsiteCourseDetail);
        if (num > 0){
            return new Result(200, "修改成功！", null);
        } else {
            return new Result(10006, "修改失败！", null);
        }
    }

//    新增推荐课程时，获取所有已上架课程信息以及条件查询
    public Result getEduCourse(EduCourse eduCourse){
//        获取所有已上架课程信息
        List<EduCourse> list = eduCourseMapper.getEduCourse(eduCourse);
        List<EduCourseDto> listd = new ArrayList<EduCourseDto>();
        List<EduWebsiteCourseDetail> listEdu = eduWebsiteCourseDetailMapper.getList();
        for (EduCourse edu:list){
//            继承类
            EduCourseDto eduCourseDto = new EduCourseDto();
//            根据专业ID获取专业名称
            String name = sysSubjectMapper.selectByPrimaryKey(Integer.valueOf(edu.getSubjectLink())).getSubjectName();
//            将单个数据赋值给继承类
            eduCourseDto.setName(name);
            BeanUtils.copy(edu, eduCourseDto);
            listd.add(eduCourseDto);
        }
        if (CollectionUtils.isEmpty(listd)){
            return new Result(10006, "查询失败,暂无数据！", null);
        } else {
            return new Result(200, "查询成功！", listd);
        }
    }

//    新增推荐课程数据
    public Result AddDetail(String courseId){
        int count = eduWebsiteCourseDetailMapper.delAll();
        String[] arr = courseId.split(",");
        List<EduWebsiteCourseDetail> list = new ArrayList<EduWebsiteCourseDetail>();
        for (int i=0; i<arr.length; i++){
            EduWebsiteCourseDetail eduWebsiteCourseDetail = new EduWebsiteCourseDetail();
            eduWebsiteCourseDetail.setCourseId(Integer.valueOf(arr[i]));
//        新增的推荐课程排序值为0
            eduWebsiteCourseDetail.setOrderNum(0);
//        次字段暂时无用，设置为0
            eduWebsiteCourseDetail.setRecommendId(0);
            list.add(eduWebsiteCourseDetail);
        }
        int num = eduWebsiteCourseDetailMapper.save(list);
        if (num > 0){
            return new Result(200, "添加成功！", null);
        } else {
            return new Result(10009, "添加失败！", null);
        }
    }


}
